package cn.edu.scnu.semart.message.controller;

import cn.edu.scnu.semart.model.chat.ChatMessage;
import cn.edu.scnu.semart.message.service.ChatMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;

@Controller
public class ChatWebSocketController {
    @Autowired
    private ChatMessageService chatMessageService;
    @Autowired
    private SimpMessagingTemplate messagingTemplate;

    // 客户端发送到/app/chat.sendMessage
    @MessageMapping("/chat.sendMessage")
    public void sendMessage(@Payload ChatMessage chatMessage) {
        // 1. 消息先存入Redis
        chatMessageService.saveMessageToRedis(chatMessage);
        // 2. 推送到目标用户（假设目标用户ID为sellerId或buyerId，实际可根据业务调整）
        String destination = "/queue/messages/" + chatMessage.getConversationId();
        messagingTemplate.convertAndSend(destination, chatMessage);
    }
} 